package com.yf.yfmovieapp.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yf.yfmovieapp.pojo.DDUser;
import com.yf.yfmovieapp.pojo.vo.FrontUserInfo;
import com.yf.yfmovieapp.service.DDUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;

/**
 * @Author Yang_Vincent
 * @Date 2020/8/29 9:39
 */
@RestController
@Slf4j
@RequestMapping("/api")
public class UserController {

    @Autowired
    private DDUserService ddUserService;

//    @GetMapping("/qiandao")
//    public R singSuccess(
//            @RequestParam("uid")String uid
//    ){
//        log.info(uid);
//        return R.ok("杨Sir,友情提示签到成功!");
//    }

    @PostMapping("/getinfo")
    public DDUser getOk(
            @RequestParam(value = "username",required = false)String username
    ){

        DDUser userInfo = this.ddUserService.getUserInfo(username);

        return userInfo;
    }

    @PostMapping("/login")
    public String loginOkV2(
            @RequestParam("username")String username,
            @RequestParam("password")String password,
            HttpServletResponse response
    ) {

        QueryWrapper<DDUser> wrapper = new QueryWrapper<>();

        wrapper.eq("username", username);
        wrapper.eq("password", password);

        DDUser user = this.ddUserService.getLoginUserInfo(username, password);

        FrontUserInfo frontUserInfo=null;
//        OutputStream out;
//        FileOutputStream out = null;

//        try {
//
//            out = new FileOutputStream("wanli-app/src/main/resources/res");
//            out.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
//            out.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
//            out.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
//            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
//            BufferedWriter bw = new BufferedWriter(osw);
//
//
//            bw.close();
////            br.close();
//        } catch (IOException e) {
//            System.out.println(e.toString());
//        }

        PrintWriter pw = null;
        String s = null;
        if (user != null){

            frontUserInfo = new FrontUserInfo();

            BeanUtils.copyProperties(user, frontUserInfo);

            frontUserInfo.setParent1_rate(user.getParent1Rate());
            frontUserInfo.setParent2_rate(user.getParent2Rate());
            frontUserInfo.setParent3_rate(user.getParent3Rate());
            frontUserInfo.setAds(new ArrayList());
            frontUserInfo.setXj_cnt(user.getXjCnt());
            //frontUserInfo.setExpirationDate(LocalDateTime.now().plusYears(1).toEpochSecond(ZoneOffset.of("+8")));

            s = JSONUtil.toJsonStr(frontUserInfo);

            log.info("当前登录用户为:{},登录时间为:{}",username,LocalDateTime.now());

        }else {
            s="110";
            log.info("当前登录用户为:{} 登录失败,时间为:{}",username,LocalDateTime.now());
        }

        response.setHeader("Content-Type","text/html;charset=UTF-8");

        byte[] bytes = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF, (byte) 0xEF, (byte) 0xBB, (byte) 0xBF, (byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
//        BufferedWriter bw= null;
//        try {
//
//            FileOutputStream fos = new FileOutputStream("wanli-app/src/main/resources/res");
//            fos.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
//            fos.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
//            fos.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
//            fos.write(s.getBytes(StandardCharsets.UTF_8));
//            OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
//            bw = new BufferedWriter(osw);
//
//            bw.flush();
//            bw.close();
////            br.close();
//        } catch (IOException e) {
//            System.out.println(e.toString());
//        }

        try {
            pw = response.getWriter();
            pw.write(new String(bytes) + s);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            pw.close();
        }

//        FileInputStream outfile =null;
//        BufferedReader br=null;
//        try {
//            outfile = new FileInputStream("wanli-app/src/main/resources/res");
//            InputStreamReader isr = new InputStreamReader(outfile, StandardCharsets.UTF_8);
//            br = new BufferedReader(isr);
//
//            log.info("当前登录用户为:{} 登录失败,时间为:{}",username,LocalDateTime.now());
//            pw = response.getWriter();
//            pw.write(new String(bytes) + s);
//            pw.flush();
////            return br;
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } finally {
//            outfile.close();
//
//        }
        return null;
    }

//    @GetMapping("/register")
//    public R registerSuccess(
//            @RequestParam("name")String username,
//            @RequestParam("password")String password
//    ){
//        User user = new User();
//
//        user.setTime(LocalDateTime.now().plusYears(8));
//        user.setShiYong(36);
//        user.setPower("3");
//        user.setShare("36");
//        user.setJe(3);
//        user.setDs("3");
//        user.setUsername(username);
//        user.setPassword(password);
//
//        this.userService.save(user);
//
//        return R.ok("注册成功");
//    }
//    @GetMapping("/verify")
//    public R registerAndLogn(
//            @RequestParam("username")String username,
//            @RequestParam("password")String password
//    ){
//
//        log.info("当前注册并登录用户为:{},登录时间为:{}",username,LocalDateTime.now());
//
//        User user = new User();
//
//        user.setTime(LocalDateTime.now().plusYears(8));
//        user.setShiYong(36);
//        user.setPower("3");
//        user.setShare("36");
//        user.setJe(3);
//        user.setDs("3");
//        user.setUsername(username);
//        user.setPassword(password);
//
//        this.userService.save(user);
//
//        FrontUserInfo frontUserInfo = new FrontUserInfo();
//
//        BeanUtils.copyProperties(user, frontUserInfo);
//
//        return R.ok(frontUserInfo);
//    }
//
//    @GetMapping("/imei")
//    public R getImeiId(
//            @RequestParam("uid")String uid
//    ){
//        User byId = this.userService.getById(uid);
//        if (byId != null){
//            FrontUserInfo frontUserInfo = new FrontUserInfo();
//
//            BeanUtils.copyProperties(byId, frontUserInfo);
//
//            return R.ok(frontUserInfo);
//        }
//        return R.fail("用户效验失败");
//    }
//    @GetMapping("/version/info")
//    public AppVersionInfo getVersionInfoOk(){
//
//        return AppVersionInfo.infoOk();
//    }

//    @GetMapping
//    public   String GetUTF8String(byte[] buffer)
//    {
//        if (buffer == null) return null;
//        if (buffer.Length <= 3)
//        {
//            return Encoding.UTF8.GetString(buffer);
//        }
//        byte[] bomBuffer = new byte[] { 0xef, 0xbb, 0xbf };
//        if (buffer[0] == bomBuffer[0] && buffer[1] == bomBuffer[1] && buffer[2] == bomBuffer[2])
//        {
//            return new UTF8Encoding(false).GetString(buffer, 3, buffer.Length - 3);
//        }
//        return Encoding.UTF8.GetString(buffer);
//    }


}
